import { useAuthStore } from '@/store/index'
import { createRouter, createWebHashHistory } from 'vue-router'

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: 'login' */ '@/views/page-login.vue')
  },
  {
    path: '/home',
    name: 'home',
    component: () => import(/* webpackChunkName: 'home' */ '@/views/page-home.vue')
  },
  {
    path: '/poetry-images',
    name: 'poetry-images',
    component: () => import(/* webpackChunkName: 'poetry-images' */ '@/views/poetry-images.vue')
  },
  {
    path: '/poetry-list',
    name: 'poetry-list',
    component: () => import(/* webpackChunkName: 'poetry-list' */ '@/views/poetry-list.vue')
  },
  {
    path: '/poetry-detail/:id',
    name: 'poetry-detail',
    component: () => import(/* webpackChunkName: 'poetry-detail' */ '@/views/poetry-detail.vue')
  },
  {
    path: '/peotry-detail/:id',
    redirect: {
      name: 'poetry-detail'
    }
  },
  {
    path: '/poetry-edit/:id',
    name: 'poetry-edit',
    component: () => import(/* webpackChunkName: 'poetry-edit' */ '@/views/poetry-edit.vue')
  },
  {
    path: '/personal',
    name: 'personal',
    component: () => import(/* webpackChunkName: 'personal' */ '@/views/page-personal.vue'),
    meta: {
      auth: false // 查看个人诗词概况不需要鉴权
    }
  },
  {
    path: '/my-msgs',
    name: 'my-msgs',
    component: () => import(/* webpackChunkName: 'my-msgs' */ '@/views/my-msgs.vue'),
    meta: {
      auth: false
    }
  },
  {
    path: '/my-resume',
    name: 'myResume',
    component: () => import(/* webpackChunkName: 'my-resume' */ '@/views/my-resume.vue'),
    meta: {
      auth: false
    }
  },
  {
    path: '/api-manage',
    name: 'api-manage',
    component: () => import(/* webpackChunkName: 'api-manage' */ '@/views/api-manage.vue'),
    meta: {
      auth: true
    }
  },
  {
    path: '/poetry-census',
    name: 'poetry-census',
    component: () => import(/* webpackChunkName: 'poetry-census' */ '@/views/poetry-census.vue'),
    meta: {
      auth: true
    }
  },
  {
    path: '/sg-components',
    name: 'sg-components',
    component: () => import(/* webpackChunkName: 'sg-components' */ '@/views/sg-components.vue')
  },
  {
    path: '/page-invalid/:invalidType',
    name: 'page-invalid',
    component: () => import(/* webpackChunkName: 'page-invalid' */ '@/views/page-invalid.vue')
  },
  {
    path: '/:catchAll(.*)',
    redirect: '/home'
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to: any, from: any, next: any) => {
  const authStore = useAuthStore()
  if (to.meta.auth && !authStore.isLogin) {
    window._sgGlobal.$toastLogin()
    if (!from.name) {
      next({ name: 'login', query: { redirect: to.fullPath } })
    } else {
      next(false)
    }
    return
  }
  next()
})

router.onError(function (e: any) {
  const { name } = e || {}
  switch (name) {
    case 'ChunkLoadError':
      window._sgGlobal.$toast('资源文件加载失败，请重试')
      break
    default:
      break
  }
})

// 优化路由返回上一级不存在的情况
router._go = router.go
router.go = function (num: number) {
  // console.log(`go, num=${num}, fromFullpath=${this._fromFullpath}`)
  if (num === -1 && history.length <= 1) {
    this.push('/')
  } else {
    this._go(num)
  }
}

export default router
